#include "hlavniokno.h"
#include <QToolBar>
#include <QApplication>
#include <QIcon>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QStatusBar>
#include <QTextEdit>
#include <QDockWidget>
#include <src/hrac.h>


HlavniOkno::HlavniOkno(QWidget *parent)
    : QMainWindow(parent)
{
    this->setWindowIcon(QIcon("logo.ico"));
    QPixmap newpix("new.png");
    QPixmap openpix("open.png");
    QPixmap quitpix("quit.png");

    QAction *quit = new QAction("&Quit", this);

    QMenu *file;
    file = menuBar()->addMenu("&Hra");
    file->addAction(quit);

    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));

    QToolBar *toolbar = addToolBar("main toolbar");
    toolbar->addAction(QIcon(newpix), "Nová hra");
    toolbar->addAction(QIcon(openpix), "Nastavení");
    toolbar->addSeparator();
    QAction *quit2 = toolbar->addAction(QIcon(quitpix),"Ukončit");

    Papir * papirek = new Papir(this);



    setCentralWidget(papirek);
    dock = new QDockWidget("Hráči", this);

    Status * neco = new Status(dock,papirek->vratHrace());
    dock->setWidget(neco);
    dock->setAllowedAreas(Qt::RightDockWidgetArea | Qt::LeftDockWidgetArea);
    dock->setFeatures(QDockWidget::DockWidgetMovable);
    addDockWidget(Qt::RightDockWidgetArea, dock);


    /* Chat */
    chat = new QDockWidget("Chat", this);

    Chat * neco2 = new Chat(chat,papirek->vratHrace());
    chat->setWidget(neco2);
    chat->setAllowedAreas(Qt::BottomDockWidgetArea);
    chat->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable);
    chat->setMaximumHeight(150);
    addDockWidget(Qt::BottomDockWidgetArea, chat);

    connect(papirek, SIGNAL(newChatMessage(QString)), neco2, SLOT(getChatMessage(QString)));
    connect(neco2, SIGNAL(newChatMessage(QString)), papirek, SLOT(onNewMessage(QString)));
    connect(papirek, SIGNAL(chatVisible(bool)), this,SLOT(chatVisible(bool)));



    statusBar()->addWidget(&statusMessage);

    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(quit2, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(papirek, SIGNAL(playerChanged(Hrac*)), this, SLOT(setStatus(Hrac*)));
}

void HlavniOkno::setStatus(Hrac * hrac)
{
    statusMessage.setText(QString("Na tahu je %1").arg(*hrac->zjistiJmeno()));
}

void HlavniOkno::chatVisible(bool status)
{
    if(status != false)
        QMessageBox::information(this, tr("Pisqorky"),"Pokus o zavreni...");
    else
        this->chat->show();
}
